//
//  Music_page_list_vc.m
//  ainkjj
//
//  Created by MAX on 2020/5/12.
//  Copyright © 2020 zhiyiLiu. All rights reserved.
//

#import "Music_page_list_vc.h"
#import <HWPanModal/HWPanModal.h>
#import "Music_list_vc.h"

#import "GetMusicReq.h"
#import "GetMusic_category_list.h"

@interface Music_page_list_vc ()<HWPanModalPresentable>
@property (nonatomic, strong)NSMutableArray *titlesArr;
@property (nonatomic, strong)NSMutableArray *classNamesArr;
@property (nonatomic, strong) UIButton *btnCancel;
@end

@implementation Music_page_list_vc

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}


- (instancetype)init

{
    self = [super init];
    if (self) {
        self.progressWidth = 40;
        self.titleColorSelected = themeColor;
        self.titleColorNormal = UIColor.darkGrayColor;
        self.titleSizeNormal = 17;
        self.titleSizeSelected = 20;
        [self setMenuViewStyle:WMMenuViewStyleLine];
        [self setPreloadPolicy:WMPageControllerPreloadPolicyNear];
 
        [self getMusicCategory];
        

     
        self.btnCancel = [[UIButton alloc] init];
        [self.btnCancel setBackgroundColor:UIColor.whiteColor];
        [self.view addSubview:self.btnCancel];

        [self.btnCancel setTitle:@"关闭" forState:UIControlStateNormal];
        [self.btnCancel setTitleColor:UIColor.blackColor forState:UIControlStateNormal];
        [self.btnCancel addTarget:self action:@selector(close) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return self;
}
- (void)close{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    self.btnCancel.frame = CGRectMake(0,self.view.bounds.size.height
                                      - (50 + Lzy_TabbarSafeBottomMargin) , ScreenWidth, 50);
}
- (void)setMusicCategory:(MusicCategory *)musicCategory{
    _musicCategory = musicCategory;
    //        [self getMusicCategory];
    NSMutableArray *titlesArr = NSMutableArray.new;
    NSMutableArray *clsNameArr = NSMutableArray.new;
    NSMutableArray *keysArr = NSMutableArray.new;
    NSMutableArray *valuesArr = NSMutableArray.new;
    for (MusicCategoryData *ac in self.musicCategory.data) {
        [titlesArr addObject:ac.name];
        [clsNameArr addObject:@"Music_list_vc"];
        [keysArr addObject:@"category_id"];
        [valuesArr addObject:@(ac.ID).stringValue];
    }
    self.titlesArr = titlesArr;
    self.classNamesArr = clsNameArr;
    self.keys = keysArr;
    self.values = valuesArr;
    [self reloadData];
}

#pragma mark - WMPageController DataSource
- (NSInteger)numbersOfChildControllersInPageController:(WMPageController *)pageController {
    return self.titlesArr.count;
}

- (NSString *)pageController:(WMPageController *)pageController titleAtIndex:(NSInteger)index {
    return self.titlesArr[index];
}

- (UIViewController *)pageController:(WMPageController *)pageController viewControllerAtIndex:(NSInteger)index {
    UIViewController *vc = nil;
    vc = [NSClassFromString(self.classNamesArr[index]) new];
    
    return vc;
}
-(CGRect)pageController:(WMPageController *)pageController preferredFrameForMenuView:(WMMenuView *)menuView{
    CGFloat originY =  CGRectGetMaxY(self.navigationController.navigationBar.frame);
    
    return  CGRectMake(0,  originY, [UIScreen mainScreen].bounds.size.width, 50);
}
- (CGRect)pageController:(WMPageController *)pageController preferredFrameForContentView:(WMScrollView *)contentView{
    CGFloat originY = CGRectGetMaxY([self pageController:pageController preferredFrameForMenuView:self.menuView]);
    
    return  CGRectMake(0,  originY, self.view.bounds.size.width,self.view.bounds.size.height - originY - (50 + Lzy_TabbarSafeBottomMargin));
    
}
#pragma mark - ================== getMusicCategory ==================
- (void)getMusicCategory{
    
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    
//    [PPHTTPRequest req:@"api/app/getMusicCategory" parameters:dict success:^(id response) {
//        NSString *error_code = [NSString stringWithFormat:@"%@", response[@"error_code"]];
//        if ([error_code isEqualToString:@"0"]) {
//            MusicCategory *musicCategory = [MusicCategory mj_objectWithKeyValues:response];
//            self.musicCategory = musicCategory;
//
//        }
//
//        else
//            //            if ([error_code isEqualToString:@""])
//        {
//
//            [MBProgressHUD showTipMessageInView:response[@"error_msg"]];
//
//        }
//    } failure:^(NSError *error) {
//        [MBProgressHUD showTipMessageInView:[error localizedDescription]];
//
//    }];
    
    
    GetMusicReq *api =[[GetMusicReq alloc] init];
    [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        // 你可以直接在这里使用 self
        DLog(@"succeed");
        NSDictionary *response = request.responseJSONObject;
        NSString *error_code = [NSString stringWithFormat:@"%@", response[@"error_code"]];
        if ([error_code isEqualToString:@"0"]) {
            MusicCategory *musicCategory = [MusicCategory mj_objectWithKeyValues:response];
            self.musicCategory = musicCategory;
            
        }
        
        else
            //            if ([error_code isEqualToString:@""])
        {
            
            [MBProgressHUD showTipMessageInView:response[@"error_msg"]];
            
        }
        
        
    } failure:^(YTKBaseRequest *request) {
        // 你可以直接在这里使用 self
        DLog(@"failed");
        [MBProgressHUD showTipMessageInView:[request.error localizedDescription]];

    }];
}
#pragma mark - HWPanModalPresentable
- (PanModalHeight)longFormHeight {
    return PanModalHeightMake(PanModalHeightTypeMaxTopInset, 140);
}
- (BOOL)allowsDragToDismiss {
    return NO;
}
- (BOOL)showDragIndicator{
    return NO;
}
-(BOOL)isUserInteractionEnabled{
    return YES;
}
-(BOOL)isPanScrollEnabled{
    return NO;
}
- (BOOL)shouldRespondToPanModalGestureRecognizer:(UIPanGestureRecognizer *)panGestureRecognizer {
    // 如果拖拽的点在navigation bar上，则返回yes，可以拖拽，否则只能滑动tableView
    CGPoint loc = [panGestureRecognizer locationInView:self.view];
    //    if (CGRectContainsPoint(self.navView.frame, loc)) {
    //        return YES;
    //    }
    return NO;
}
@end
